package com.platform.util.wechat;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
import cn.binarywang.wx.miniapp.message.WxMaMessageRouter;
import com.platform.util.WxConfigUtils;

/**
* 微信接口工厂类
* @author luojh
* @version $Id: WxMpServiceFactory.java, v 0.1 2018年6月28日 下午10:58:21 luojh Exp $
*/
public class WxMpServiceFactory {

    private static WxMaService instance;

    private static WxMaInMemoryConfig wxMaConfigStorage;

    private static WxMaMessageRouter router = new WxMaMessageRouter(getInstance());
    
 

    private WxMpServiceFactory() {

    }

    // 产生WxmaService实例
    public static WxMaService getInstance() {

        if (wxMaConfigStorage == null) {
            
            wxMaConfigStorage = new WxMaInMemoryConfig();
            
            wxMaConfigStorage.setAppid(WxConfigUtils.getAppid());
            wxMaConfigStorage.setSecret(WxConfigUtils.getAppsecret());
            wxMaConfigStorage.setToken(WxConfigUtils.getToken());
            wxMaConfigStorage.setAesKey(WxConfigUtils.getAeskey());
            wxMaConfigStorage.setMsgDataFormat(WxConfigUtils.getMsgDataFormat());

        }

        if (instance == null) {
            instance = new WxMaServiceImpl();
            instance.setWxMaConfig(wxMaConfigStorage);
        }
   

        return instance;
    }
    
    

    public static WxMaInMemoryConfig getWxMaConfigStorage() {
        return wxMaConfigStorage;
    }

    public static void setWxMaConfigStorage(WxMaInMemoryConfig wxMpConfigStorage) {
        WxMpServiceFactory.wxMaConfigStorage = wxMpConfigStorage;
    }

    public static void setInstance(WxMaService instance) {
        WxMpServiceFactory.instance = instance;
    }

    public static WxMaMessageRouter getRouter() {
        return router;
    }

    public static void setRouter(WxMaMessageRouter router) {
        WxMpServiceFactory.router = router;
    }

}